import React, { useEffect, useState } from 'react'

function Child() {
  useEffect(()=>{
    window.onresize = ()=>{
        console.log("resize");
    };
    let clock = setInterval(()=>{
        console.log(Math.random())
    },1000)
    return ()=>{     //在useEffect匿名函数的最后加上return ()=>{},相当于类组件中的 componentwillUnmount()
        window.onresize = null;
        clearInterval(clock);
    }
  },[])
  return (
    <div>
        我出来啦！
    </div>
  )
}

export default function App() {
  const [change, setchange] = useState(true)
  return (
    <div>
        <button onClick={()=>{
            setchange(!change);
        }}>click</button>
        {change && <Child></Child>}
    </div>
  )
}
